package com.szt.sy.controller.api.open;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.szt.sy.controller.base.ApiBaseController;
import com.szt.sy.entity.blog.Article;
import com.szt.sy.entity.blog.ArticleLeftMessage;
import com.szt.sy.service.blog.article.ArticleService;
import com.szt.sy.service.blog.articleleftmessage.ArticleLeftMessageService;
import com.szt.sy.vo.rep.base.RepBaseVO;
import com.szt.sy.vo.rep.base.RepPageVO;

/***
 * 接口-开放-留言
 * 
 * @author wwy
 *
 */
@RestController
@RequestMapping("api/open/article/leftmessage")
public class ApiOpenArticleLeftMessageController extends ApiBaseController {
	
	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private ArticleLeftMessageService articleLeftMessageService;
	
	@PostMapping("page")
	public RepPageVO<List<ArticleLeftMessage>> articlePage(Integer currentPage, Integer pageSize, Long id) {
		RepPageVO<List<ArticleLeftMessage>> repVO = new RepPageVO<>();
		Page<ArticleLeftMessage> page = new Page<>(currentPage, pageSize);
		Wrapper<ArticleLeftMessage> wrapper = new EntityWrapper<ArticleLeftMessage>().orderBy("id", false);
		wrapper.eq("articleId", id);
		Page<ArticleLeftMessage> pageInfo = articleLeftMessageService.selectPage(page, wrapper);
		List<ArticleLeftMessage> data = pageInfo.getRecords();
		repVO.setCommonSuccess();
		repVO.setData(data);
		repVO.setCurrentPage(pageInfo.getCurrent());
		repVO.setTotalPage(pageInfo.getPages());
		repVO.setTotalResult(page.getTotal());
		return repVO;
	}
	
	@PostMapping("add")
	public RepBaseVO addLeftMessage(Long id, String content) {
		RepBaseVO repVO = new RepBaseVO<>();
		Integer num = articleService.selectCount(new EntityWrapper<Article>().eq("id", id));
		if (num.equals(0)) {
			return repVO.setCommonFail().setMes("文章不存在");
		}
		ArticleLeftMessage leftMessage = new ArticleLeftMessage(id, content);
		articleLeftMessageService.addRecord(leftMessage);
		
		return repVO.setCommonSuccess().setData(leftMessage);
	}
}
